/**
 * 图片层
 */
require('node-easel');
var ImgLayer = function(layer){
	this._layer = layer;
	this._imgKeyList = {};
	/**
	 * 设置图片map
	 * @param {String} src map的Key
	 * @param {Object} obj 要缓存的图片
	 */
	this._setImgMap = function(src,obj){
		this._imgKeyList[src] = obj;
	};

	/**
	 * 得到图片map
	 * @param {String} src map的Key
	 */
	this._getImgMap = function(src){
		return this._imgKeyList[src];
	};

	/**
	 * 画画
	 * @param {Object} imageObj 要画的对象
	 */
	this.draw = function(imageObj){
		//如果isAdd为0则不画出来
		if(!imageObj.isAdd){
			var myObj = this._getImgMap(imageObj.relativeSourcePath);
			this._layer.removeChild(myObj);
			this._setImgMap(imageObj.relativeSourcePath,null);
		}else{
			var myObj = new createjs.Bitmap(imageObj.imageObj);
			var angle = parseInt(Math.round(imageObj.angle*180/Math.PI));
			//TODO 白板语言的bug！！！！
			if((angle / 90) % 2 == 0){
				myObj.set({
					regX : imageObj.imageObj.width/2,
					regY : imageObj.imageObj.height/2,
					x:imageObj.x + imageObj.width/2,
					y:imageObj.y + imageObj.height/2,
					rotation :imageObj.angle*180/Math.PI,
					scaleX : imageObj.width/imageObj.imageObj.width,
					scaleY : imageObj.height/imageObj.imageObj.height
				});
			}else{
				myObj.set({
					regX : imageObj.imageObj.width/2,
					regY : imageObj.imageObj.height/2,
					x:imageObj.x + imageObj.width/2,
					y:imageObj.y + imageObj.height/2,
					rotation :imageObj.angle*180/Math.PI,
					scaleX : imageObj.height/imageObj.imageObj.width,
					scaleY : imageObj.width/imageObj.imageObj.height
				});
			}

			this._layer.addChild(myObj);
			this._setImgMap(imageObj.relativeSourcePath,myObj);
		}
	}

	/**
	 * 清空
	 */
	this.clear = function(){
		this._layer.removeAllChildren();
	}

	/**
	 * 得到图片们
	 */
	this.getImgs = function(){
		return this._imgKeyList;
	}

	/**
	 * 设置背景图
	 * @param {Object} bitmaps 要设置的bitmap们
	 */
	this.setImgs = function(bitmaps){
		for(var x in bitmaps){
			if(!bitmaps[x])
				return;

			var bitmap = bitmaps[x].clone();
			this._layer.addChild(bitmap);
			this._setImgMap(x,bitmap);
		}
	}
};
module.exports = ImgLayer;